package com.my.auth.service.service.impl;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.alibaba.nacos.shaded.com.google.gson.reflect.TypeToken;
import com.my.auth.common.enums.IsDeletedFlagEnum;
import com.my.auth.service.dao.AuthPermissionDao;
import com.my.auth.service.dao.AuthRolePermissionDao;
import com.my.auth.service.dao.AuthUserRoleDao;
import com.my.auth.service.entity.AuthPermission;
import com.my.auth.service.entity.AuthRolePermission;
import com.my.auth.service.entity.AuthUserRole;
import com.my.auth.service.service.AuthPermissionService;
import com.my.auth.utils.RedisUtil;
import com.my.auth.utils.TreeUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (AuthPermission)表服务实现类
 *
 * @author makejava
 * @since 2023-11-03 00:45:50
 */
@Service("authPermissionService")
public class AuthPermissionServiceImpl implements AuthPermissionService {
    @Resource
    private AuthRolePermissionDao authRolePermissionDao;
    @Resource
    private AuthPermissionDao authPermissionDao;
    @Resource
    private AuthUserRoleDao authUserRoleDao;


    private static final String authPermissionPrefix = "auth.permission";

    @Override
    public Boolean add(AuthPermission authPermission) {
        authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = authPermissionDao.insert(authPermission);
        return count > 0;
    }

    @Override
    public Boolean update(AuthPermission authPermission) {
        int count = authPermissionDao.update(authPermission);
        return count > 0;
    }

    @Override
    public Boolean delete(AuthPermission authPermission) {
        authPermission.setId(authPermission.getId());
        authPermission.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = authPermissionDao.update(authPermission);
        return count > 0;
    }

    @Override
    public List<String> getPermissionKey(String userName) {
        String permissionKey = RedisUtil.buildKey(authPermissionPrefix, userName);
        String permissionValue = RedisUtil.get(permissionKey);
        if (StringUtils.isBlank(permissionValue)) {
            return Collections.emptyList();
        }
        List<AuthPermission> permissionList = new Gson().fromJson(permissionValue,
                new TypeToken<List<AuthPermission>>() {
                }.getType());
        return permissionList.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());
    }

    @Override
    public List<AuthPermission> getPermission(Long loginId) {
        AuthUserRole authUserRole = authUserRoleDao.queryById(loginId);
        Long roleId = authUserRole.getRoleId();

        AuthRolePermission authRolePermission = new AuthRolePermission();
        authRolePermission.setRoleId(roleId);
        List<AuthRolePermission> rolePermissionList = authRolePermissionDao.
                queryAllByLimit(authRolePermission);

        List<Long> permissionIdList = rolePermissionList.stream()
                .map(AuthRolePermission::getPermissionId).collect(Collectors.toList());
        //根据roleId查权限
        List<AuthPermission> authPermissionList = authPermissionDao.queryByRoleList(permissionIdList);
        //构建树形结构
        return TreeUtil.buildTree(authPermissionList, AuthPermission::getId,AuthPermission::getParentId, 0L);

    }

}
